package com.mcxx.modules.miniSalvation.check.service.write;

import com.mcxx.modules.miniSalvation.check.dao.read.CheckReadDao;
import com.mcxx.modules.miniSalvation.check.dao.write.CheckWriteDao;
import com.mcxx.modules.miniSalvation.family.dao.read.FamilyMemberReadDao;
import com.mcxx.modules.miniSalvation.order.dao.read.OrderCertReadDao;
import com.mcxx.modules.miniSalvation.order.dao.write.OrderWriteDao;
import com.mcxx.modules.miniSalvation.order.dto.OrderDTO;
import com.mcxx.modules.miniSalvation.order.entity.OrderStateEntity;
import com.mcxx.modules.temporary.order.dao.write.TmpOrderWriteDao;
import com.mcxx.modules.temporary.order.dto.TmpOrderDTO;
import com.mcxx.util.Constant;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
@TaTransactional
public class SZTCheck  extends BaseService {

    @Autowired
    private CheckWriteDao checkWriteDao;
    @Autowired
    private OrderWriteDao orderWriteDao;
    @Autowired
    private TmpOrderWriteDao tmpOrderWriteDao;


    public void sendSZT(List<OrderDTO> orderDTOList, String userAreaLevel){

        log.info("保存数据到中台-开始");


        this.updateCheckState(orderDTOList);


        this.updateOrderState(orderDTOList, userAreaLevel);

    }

    public void updateCheckState(List<OrderDTO> orderDTOList){
        List<String> orderIdList = orderDTOList.stream().map(OrderDTO::getId).collect(Collectors.toList());
        //2、更新check表的状态,Constant.CheckRequestState.SEND='02'救助已发送信息到中台
        int rst = checkWriteDao.updateCheckState(orderIdList, Constant.CheckState.SEND);
        if(rst <= 0){
            log.info("{}已经向中台发起成功,但是check表状态更新失败", orderIdList);
        }
    }



    public void updateOrderState(List<OrderDTO> orderDTOList, String userAreaLevel){
        for(OrderDTO orderDTO : orderDTOList){
            OrderStateEntity orderStateEntity = new OrderStateEntity(orderDTO.getId(), Constant.BizStateNew.TOWN_CHECK, Constant.BizStateNew.TOWN_CHECK, orderDTO.getVersion());
            int rst = orderWriteDao.updateState(orderStateEntity);
            if(rst <= 0){
                log.info("orderId = {},说明修改状态信息失败", orderDTO.getId());
            }
        }
    }


    public int tmpSendSZT(List<TmpOrderDTO> orderDTOList, String userAreaLevel){

        log.info("保存数据到中台-开始");


        this.tmpUpdateCheckState(orderDTOList);


        this.tmpUpdateOrderState(orderDTOList, userAreaLevel);

        return 1;
    }


    public void tmpUpdateCheckState(List<TmpOrderDTO> orderDTOList){
        List<String> orderIdList = orderDTOList.stream().map(TmpOrderDTO::getId).collect(Collectors.toList());
        //2、更新check表的状态,Constant.CheckRequestState.SEND='02'救助已发送信息到中台
        int rst = checkWriteDao.updateCheckState(orderIdList, Constant.CheckState.SEND);
        if(rst <= 0){
            log.info("{}已经向中台发起成功,但是check表状态更新失败", orderIdList);
        }
    }


    public void tmpUpdateOrderState(List<TmpOrderDTO> orderDTOList, String userAreaLevel){
        String bizState = Constant.UserLevel.STREET.equals(userAreaLevel) ? Constant.BizStateTmp.TOWN_CHECK : Constant.BizStateTmp.COUNTY_CHECK;
        for(TmpOrderDTO orderDTO : orderDTOList){
            if(Constant.BizStateNew.APPROVE_REJECT.equals(orderDTO.getState())){

                continue;
            }
            OrderStateEntity orderStateEntity = new OrderStateEntity(orderDTO.getId(), bizState, null, orderDTO.getVersion());
            int rst = tmpOrderWriteDao.updateState(orderStateEntity);
            if(rst <= 0){
                log.info("orderId = {},说明修改状态信息失败", orderDTO.getId());
            }
        }
    }
}
